<?php
/**
* +---------------------------------------------------------------------------+
* | $Id: useradd.php 37 2005-04-03 05:54:36Z vluther $ |
* +---------------------------------------------------------------------------+
* | $Rev: 37 $ |
* +---------------------------------------------------------------------------+
* | Copyright (c) 2002-2005 Linuxpowered, Inc 																|
* +---------------------------------------------------------------------------+
* | Author: Vidyut Luther 																										|
* +———————————————————————————————————————————————————————————————————————————+
*
* $URL: http://svn.linuxpowered.com/binder/admin/useradd.php $
**/ 
$section = "ADMIN";
$title = "Binder: User Setup "; 
require_once '../global.inc.php'; 


?>
<div id="setupForm"><div class="title"><span class="form_title">User Setup </span> </div>
<?php
$npass = trim($_POST['password']); 
$firstname = trim($_POST['firstname']); 
$lastname = trim($_POST['lastname']); 
$email = trim($_POST['email']); 
$userlevel = $_POST['userlevel']; 
#$domains = explode("\n",$_POST['domains']) ; 
$mode = $_POST['mode']; 

$action = $_POST['action'];


if($action == "domadd") {
	require_once INCLUDE_PATH . '/FQDN.class.php'; 
	$objDom =& new FQDN();  

  if(!empty($_POST['foruser'])) { 
    $userid = $_POST['foruser']; 
  } else {
    $userid = $_SESSION['USERID']; 
  }
	$fqdn = $_POST['searchfor']; 
  $objDom->mAddDomain($userid,$fqdn); 
  header("Location: fqdnlist.php");
  exit;  
}


if(empty($email)) { 
	echo "Email address can not be empty, Please try again "; 
}
if("modify" == $mode) {
  modify_user($_POST); 
} else {

$sql = "INSERT INTO users (password,firstname,lastname,email,level) VALUES ('$npass','$firstname','$lastname','$email','$userlevel') "; 

$db->mInsert($sql); 

$userid = mysql_insert_id(); 
  
/* Now Send Email to The User */

 
$body = "
Dear $firstname $lastname,
 Your account has been created via phpBinder. Please go to 
". BASE_URL ."

You can log in to the administrative panel, by using the following 
username/password combination.



  Username: $email
  Password: $npass  


Thank you for using phpBinder.

For suggestions/questions. Please contact Vid Luther at (vid@linuxpowered.com)
"; 

#$email = "vid@linuxpowered.com" ; 
mail($email,"Your phpBinder Account",$body); 
}



function modify_user($_POST)
{
  global $db; 
  require_once INCLUDE_PATH . '/FQDN.class.php'; 
  $objDom =& new FQDN();  
  $npass = $_POST['password']; 
  $firstname = $_POST['firstname']; 
  $lastname = $_POST['lastname']; 
  $email = $_POST['email']; 
  $userlevel = $_POST['userlevel']; 
  $domains = explode("\n",$_POST['domains']) ; 
	$uid = $_POST['userid']; 
  #echo "<pre>"; 
  #print_r($_POST); 
  #$uid = $_SESSION['USERID']; 
  $sql = "UPDATE users SET password='$npass', firstname='$firstname', email='$email',lastname='$lastname' WHERE userid='$uid'";  
	
	$db->mInsert($sql);
  
  /* Now go through each domain we were passed, and match it against the domains already in the database */
  
  /* What we're doing here is: 
   for each domain given to us, check to see if it is in the fqdn table, and if it belongs to the user, 
   if it belongs to the user, then great, if it exists in the table, but doesn't belong to the user
     throw an notice and leave it. 
     otherwise, add the domain for the user and be happy. 
  */

  for($i=0; $i<count($domains);$i++) {
   $dom = rtrim($domains[$i]);
   $dom = ltrim($dom);  
   
    if(!empty($dom)) {   
      $sql = "SELECT fqdn,user FROM fqdn WHERE fqdn='$dom' "; 
      $db->mRun($sql);  
      $nr = $db->mGetNumRows() ; 
      if($nr == "1") {
         next; 
      } else {
        $objDom->mAddDomain($uid,$dom); 
        echo "Adding $dom "; 
      }
    }
    echo "<br> "; 
    
   
  }
  
   echo "<a href='userlist.php'> Back to the Userlist </a> <br>";  
}
   
#print_array($domains,"vd"); 
?>
